package com.icss.ah.evalManager.action;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.evalManager.service.EvalManagerService;
import com.icss.ah.login.bean.UserInfoBean;

/**
 * 
 * @author 
 *
 */
@Controller
@Scope("prototype")
public class EvalManagerAction extends BaseAction {
	
	@Autowired
	private EvalManagerService evalManagerService;
	
	public String toEvalListPage(){
		String returnPage = "toEvalListPage";

		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String shopId=userInfo.getShopId();
		
		request = this.getRequest();
		HashMap map  = new HashMap();
		map.put("shopId", shopId);
		map.put("corpId", corpId);
		List evalGradeList = evalManagerService.getEvalCountList("EvalManagerBean.getEvalGradeList", map);
		List eval = evalManagerService.getEvalCountList("EvalManagerBean.getEvalCountList", map);
		long all = 0,date_7_all = 0,date_month_all = 0,date_halfyear_all = 0,date_halfyear_bef_all = 0;
		
		double hpb,zpb,cpb = 0;
		for(int i=0;i<eval.size();i++){
			Map e = (HashMap)eval.get(i);
			all += (Long)e.get("date_all");
			date_7_all+=(Long)e.get("date_7");
			date_month_all+=(Long)e.get("date_month");
			date_halfyear_all+=(Long)e.get("date_halfyear");
			date_halfyear_bef_all+=(Long)e.get("date_halfyear_bef");
		}
		
		for(int i=0;i<eval.size();i++){
			Map e = (HashMap)eval.get(i);
			double bl =  0;
			if(all>0){
				bl = ((Long)e.get("date_all"))*1.0/all*100;
			} 
			DecimalFormat df = new DecimalFormat("0.00");  
	        e.put("bl", df.format(bl));  
		}
		
		request.setAttribute("eval", eval);
		request.setAttribute("evalAll", all);
		request.setAttribute("evalDate7All", date_7_all);
		request.setAttribute("evalDateMonthyearAll", date_month_all);
		request.setAttribute("evalDateHalfyearAll", date_halfyear_all);
		request.setAttribute("evalDateHalfyearBefAll", date_halfyear_bef_all);
		request.setAttribute("evalGradeList", evalGradeList);
		return returnPage;
	}
	
	
	public void getEvalList(){
		request = this.getRequest();
		
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		
		
		String t  = request.getParameter("t")==null?"":request.getParameter("t").trim();
		String e  = request.getParameter("e")==null?"":request.getParameter("e").trim();
		String p  = request.getParameter("p")==null?"":request.getParameter("p").trim();
		
		
		//System.out.println(t+" "+e+" "+p);
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id

			map.put("time", t);
			map.put("eval", e);
			map.put("nr", p);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> list = new ArrayList();
			list = evalManagerService.getEvalList("EvalManagerBean.getEvalList", map);
			long count = evalManagerService.getEvalListCount("EvalManagerBean.getEvalListCount", map);

			for(int i=0;i<list.size();i++){
				Map m = (HashMap)list.get(i);
				String eval = (String)m.get("EVAL_VAL_ID");
				if("1001".equals(eval)){
					if(m.get("EVAL_CONTENT")==null||"".equals((String)m.get("EVAL_CONTENT"))){
						m.put("EVAL_CONTENT","好评");
					}
				}else if("1002".equals(eval)){
					if(m.get("EVAL_CONTENT")==null||"".equals((String)m.get("EVAL_CONTENT"))){
						m.put("EVAL_CONTENT","中评");
					}
				
				}else if("1003".equals(eval)){
					if(m.get("EVAL_CONTENT")==null||"".equals((String)m.get("EVAL_CONTENT"))){
						m.put("EVAL_CONTENT","差评");
					}
				}
			}
			
			//System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>productList:"+list);
			//System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>productCount:"+count);
			
			dataGridJson.setRows(list);
			dataGridJson.setTotal(count);
			result = JSONObject.fromObject(dataGridJson).toString();
		} catch (Exception ee) {
			ee.printStackTrace();
		} finally {
			str2Resp(result);
		}
	}
	
	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}
	
}
